package com.woniu.interceptor;

import com.woniu.service.VisitService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class VisitStatisticsInterceptor implements HandlerInterceptor {

    @Autowired
    private VisitService visitService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 排除特定路径（可选）
        String path = request.getRequestURI();
        if (path.startsWith("/static/") || path.startsWith("/error")) {
            return true;
        }

        // 记录访问
        visitService.recordVisit(request);
        return true;
    }
}